using RestaurantFinder.Core.Models;
using RestaurantFinder.Core.Services;

namespace RestaurantFinder.Infrastructure.Services;

/// <summary>
/// 餐厅服务实现（使用模拟数据）
/// </summary>
public class RestaurantService : IRestaurantService
{
    /// <summary>
    /// 搜索指定位置附近的餐厅
    /// </summary>
    public Task<List<Restaurant>> SearchRestaurantsAsync(Location userLocation, int radiusInMeters)
    {
        var mockRestaurants = GenerateMockRestaurants(userLocation);
        
        // 根据搜索半径过滤餐厅
        var filteredRestaurants = mockRestaurants
            .Where(r => r.Distance <= radiusInMeters)
            .ToList();

        return Task.FromResult(filteredRestaurants);
    }

    /// <summary>
    /// 从餐厅列表中随机选择一家
    /// </summary>
    public Restaurant? SelectRandomRestaurant(IList<Restaurant> restaurants)
    {
        if (!restaurants.Any())
            return null;

        var random = new Random();
        var index = random.Next(restaurants.Count);
        return restaurants[index];
    }

    /// <summary>
    /// 生成模拟餐厅数据
    /// </summary>
    private static List<Restaurant> GenerateMockRestaurants(Location userLocation)
    {
        return new List<Restaurant>
        {
            new("1", "川香居", "朝阳区三里屯路12号", 
                new Location(userLocation.Latitude + 0.001, userLocation.Longitude + 0.001), 
                150, 4.5, "010-12345678"),
            
            new("2", "粤味轩", "朝阳区工体北路8号", 
                new Location(userLocation.Latitude - 0.002, userLocation.Longitude + 0.003), 
                280, 4.2, "010-87654321"),
            
            new("3", "江南小厨", "朝阳区朝阳门外大街15号", 
                new Location(userLocation.Latitude + 0.003, userLocation.Longitude - 0.001), 
                350, 4.7, "010-11223344"),
            
            new("4", "老北京炸酱面", "朝阳区建国门外大街20号", 
                new Location(userLocation.Latitude - 0.001, userLocation.Longitude - 0.002), 
                420, 4.1, "010-55667788"),
            
            new("5", "意式风情", "朝阳区国贸桥东南角", 
                new Location(userLocation.Latitude + 0.002, userLocation.Longitude + 0.004), 
                500, 4.4, "010-99887766"),
            
            new("6", "火锅英雄", "朝阳区双井桥西", 
                new Location(userLocation.Latitude - 0.003, userLocation.Longitude - 0.003), 
                650, 4.6, "010-33445566"),
            
            new("7", "日式料理", "朝阳区CBD核心区", 
                new Location(userLocation.Latitude + 0.004, userLocation.Longitude - 0.002), 
                720, 4.3, "010-77889900"),
            
            new("8", "韩式烤肉", "朝阳区望京SOHO", 
                new Location(userLocation.Latitude - 0.004, userLocation.Longitude + 0.005), 
                850, 4.8, "010-55443322"),
                
            new("9", "西北面馆", "朝阳区大望路地铁站", 
                new Location(userLocation.Latitude + 0.005, userLocation.Longitude - 0.003), 
                920, 4.0, "010-66778899"),
                
            new("10", "泰式餐厅", "朝阳区国贸商城", 
                new Location(userLocation.Latitude - 0.005, userLocation.Longitude + 0.002), 
                1100, 4.6, "010-44556677")
        };
    }
} 